<?php 
require_once "config.php";
require_once "dbconfig.php";
require_once "computationHelper.php";
function CheckNum($numString) { 
	if (!ereg("[0-9.]$", $numString)) return false; 
	return true; 
} 

function CheckValidate($numString,$smallString,$bigString) { 
	if($numString < $smallString || $numString > $bigString) return false;
	return true; 
} 

$uidCount =0;
$sqlQuery = "select * from app_pp_user_health where status ='1' and out_id=".$uid;
$uidResult = mysql_db_query($database, $sqlQuery);
$uidCount = mysql_num_rows($uidResult); 
$uidInfo = mysql_fetch_object($uidResult);
$method = null;
if($_POST["method"]){
	$method = $_POST["method"];
}else{
	$method = $_GET["method"];
}
$countNum = 0;
$isErrorHeight = false;
$isErrorWeight = false;
 if($method=="insert"){
	$sex = $_POST['sex'];
	$age = $_POST['age'];
	$height = $_POST['height'];
	$weight = $_POST['weight'];
	$user_name = $_POST['userName'];
	$head_url = $_POST['headUrl'];
	$out_id = $_POST['outId'];
	$university_name = $_POST['universityName'];
	$company_name = $_POST['companyName'];
	if(!CheckNum($height) || !CheckValidate($height,100,250)){
		$isErrorHeight = true;
	}
	if(!CheckNum($weight) || !CheckValidate($weight,30,140)){
		$isErrorWeight = true;
	}
	#$sqlQuery = "select count(*) from app_pp_user_health where status ='1' and out_id=".$out_id;
	#$countNum = mysql_db_query($database, $sqlQuery);
	if($uidCount == 0 && !$isErrorHeight && !$isErrorWeight){
		$computationHelper = new ComputationHelper($sex,$age,$height,$weight); 
		$computationHelper->isStandard();
		$superRank = $computationHelper->getSuperRank();
		$healthy_index = $computationHelper->getBMI();
		$sqlInsert = "insert into app_pp_user_health(age,sex,height,weight,type,out_id,super_rank,user_name,head_url,healthy_index,university_name,company_name,status) values(".$age.",'".$sex."',".$height.",".$weight.",'x','".$out_id."','".$superRank."','".$user_name."','".$head_url."','".$healthy_index."','".$university_name."','".$company_name."','1')";
		$flag = mysql_db_query($database, $sqlInsert);
		require_once('xiaonei.class.php');
		$xn = new XNapp($apiKey,$secret);
		$params = array (
			"template_id"		=> 1,
			"title_data"		=> '{"actor":'.$user_name.'}', //这个参数的传入必须是json格式
			"body_data"			=> '{"name":'.$user_name.',"BMI":'.$healthy_index.'}' //这个参数的传入必须是json格式
		);
		$xn->feed('publishTemplatizedAction',$params);

	}
	#header("Location: http://apps.xiaonei.com/dongdong/");
}

$tab = "home";
require_once "tab.inc.php";
?>

<div style="float:left;padding:1px;overflow-y:no;width:313px;height:180px;text-align:left;clear:both">
<div style="margin:5px;padding:10px;">
<h4>"你比我胖"的游戏规则:</h4>
<ul style="list-style:none;line-height:120%;">
<li style="list-style:none">一、玩家得遵“从君子协定“，只提供真实信息</li>
<li style="list-style:none">二、本应用数据信息概不对外透露</li>
<li style="list-style:none">三、一个月只能测算一次</li>
<li style="list-style:none">四、排名以健康指数(BMI)为准则、如果你不幸入榜、请积极邀请更胖的好友来玩</li>
<li style="list-style:none">五、退出规则，请帮助邀请5个好友来玩玩</li>
</ul>
<p style="margin-top:10px;font:14px;">
  <b>能找出校内最胖的ggmm吗？</b>
</p>
<p>
   所有玩家，免费赠送一个<a href="http://apps.xiaonei.com/toolshare">百宝箱</a> <a href="http://apps.xiaonei.com/toolshare"><img src="<?php echo $_ENV['imgUrl']?>/toolshare.jpg"/></a>
</p>
</div>
</div>
<div style="float:left;padding:1px;margin-left:10px;overflow-y:no;width:320px;height:220px;text-align:left;border:0px #FFE5C0 solid;background:#FFFBF3;">
<xn:invite-form>
  <xn:multi-friend-selector max="5"/>
  <input type="submit" class="inputsubmit" value="邀请好友来量量"/>
</xn:invite-form>
</div>
<div style="clear:both"></div>
<?php 
//if($countNum <=0 ){
$headUrl;$uidSex;$userName;$companyName;$universityName;
if(!$_POST['headUrl'] && $uidCount == 0){
	$url = "http://api.xiaonei.com/restserver.do?format=JSON&api_key=".$apiKey
			."&method=xiaonei.users.getInfo&session_key=".urlencode($sk)
			."&v=1.0&fields=name,sex,headurl,work_history,university_history&uids=".$uid;

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$rt = curl_exec($ch);
	curl_close($ch);

	$temp = htmlspecialchars($rt);
	$xmlHelper = new XMLHelper($temp);
	#echo $xmlHelper->getXMLNodeValue("sex",false)."<br>";
	 $headUrl = $xmlHelper->getXMLNodeValue("headurl",true);
	 $uidSex = $xmlHelper->getXMLNodeValue("sex",false);
	 $userName = $xmlHelper->getXMLNodeValue("name",false);
	 $companyName = $xmlHelper->getXMLNodeValue("company_name",false);
	 $university = $xmlHelper->getXMLNodeValue("university_info",false);
	 $xmlHelper = new XMLHelper($university);
	 $universityName = $xmlHelper->getXMLNodeValue("name",false);
}else{
	$headUrl = $_POST['headUrl'];
	$uidSex  = $_POST['sex'];
	$userName = $_POST['userName'];
}   
?>
<div style= "float:left;margin:2px;width:315px;height:150px;background:url(<?php echo $_ENV['imgUrl']?>/box_grey_315x150_bar.gif) no-repeat;clear:both" >
   <div style="margin-left:30px;margin-top:10px;">
      <?php if($uidCount == 0){  ?>
      <form action="index.php" id="body_form" method="post">
	    <input type="hidden" name="method" value="insert"/>
		<input type="hidden" name="sex" value='<?php echo $uidSex;?>'>
		<input type="hidden" name="outId" value="<?php echo $uid;?>"/>
		<input type="hidden" name="headUrl" value="<?php echo $headUrl;?>"/>
		<input type="hidden" name="userName" value="<?php echo $userName;?>"/>
		<input type="hidden" name="universityName" value="<?php echo $universityName;?>"/>
		<input type="hidden" name="companyName" value="<?php echo $companyName;?>"/>
		<input type="hidden" name="status" value="1"/>
        <div style="color:#363636;font-weight:bold;margin-left:10px;line-height:27px;font-size:18px;">请填写以下信息：</div>
        <ul style="margin:2px 4px 4px 8px!important;margin:2px 4px 4px 4px;width:280px;">
            <li><span style="color:#FF6600;font-size:12px;">性别：<?php echo $uidSex==1?"男":"女";?></span></li>
            <li><span style="color:#FF6600;font-size:12px;">年龄 </span><input name="age" type="text" value="<?php if($_POST['age']) echo $_POST['age'];?>" /><span style="color:#FF6600;font-size:12px;">&nbsp;岁</span></li>
            <li><span style="color:#FF6600;font-size:12px;">身高 </span><input name="height" type="text" value="<?php if($_POST['height']) echo $_POST['height'];?>" /><span style="color:#FF6600;font-size:12px;">&nbsp;厘米  <?php if($isErrorHeight){ echo "请输入正确的值";}?></span></li>
            <li><span style="color:#FF6600;font-size:12px;">体重 </span><input name="weight" type="text" value="<?php if($_POST['weight']) echo $_POST['weight'];?>" /><span style="color:#FF6600;font-size:12px;">&nbsp;公斤  <?php if($isErrorWeight){ echo "请输入正确的值";}?></span></li>
            <div style="color:#363636;font-weight:bold;margin-left:10px;line-height:27px;font-size:18px;"><span style="margin-left:70px"><input type="submit" value="测算"/></span></div>
        </ul>
	</form>
	<?php }else{ 
			$helper = new ComputationHelper($uidInfo->sex,$uidInfo->age,$uidInfo->height,$uidInfo->weight); 
			$helper->isStandard();
	?>
		<ul style="margin:2px 4px 4px 8px!important;margin:2px 4px 4px 4px;width:210px;">
            <li>
			   <span style="color:#FF6600;font-size:12px;">您的BMI:<?php echo $uidInfo->healthy_index;?></span>
			   &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF6600;font-size:14px;font-weight:bold;"><a href="<?php echo $_ENV['xiaoneiUrl']."share.php";?>">去瘦身中心去转转</a>
				</span>
			</li>
			<li><span style="color:#005AB4;font-size:12px;">符合您身材的参考标准：</span></li>
            <li><span style="color:#FF6600;font-size:12px;">保持您的体重：
				<?php echo $helper->userStandardStart."--".$helper->userStandardEnd."公斤";?></span></li>          
			<li><span style="color:#FF6600;font-size:12px;">脂肪系数:&nbsp;19--24之间</span></li>  
			<li><span style="color:#FF6600;font-size:12px;">您的三围是这样？&nbsp;<?php echo "胸围(".$helper->getChestStandard().")  腰围(".$helper->getWaistStandard().")  臀围(".$helper->getHipsStandard().")";?></span></li>
			<div style="color:#363636;font-weight:bold;margin-left:10px;line-height:27px;font-size:18px;"><span style="margin-left:70px"><a href="cancelUserHealth.php">取消游戏</a></span></div>
        </ul>
	<?php  } ?>
   </div>	
</div>

 <?php 
   if($_POST['age'] && !$isErrorHeight && !$isErrorWeight){
	   $helper = new ComputationHelper($_POST['sex'],$_POST['age'],$_POST['height'],$_POST['weight']); 
?> 
<div style="float:right;margin:2px;width:315px;height:150px;">
        <div style="color:#363636;font-weight:bold;margin-left:10px;line-height:27px;font-size:18px;">健康指数：</div>
        <ul style="float:left;margin:2px 4px 4px 8px!important;">
            <li>
				<span style="color:#FF6600;font-size:12px;">质量指数(BMI):
					<?php 
						$userBMI = $helper->isStandard(); 
						$argeeHeath = $helper->getSuperRank();
						echo $helper->getBMI()."   ";
						if($argeeHeath=="heavy"){
							echo "超重。";
						}elseif($argeeHeath == "light"){
							echo "太轻了，舍不得吃？小小年纪多吃点。";
						}else{
							echo "你的身材保持的很不错！";
						}
					 ?>
				</span>
				<span style="color:#FF6600;font-size:14px;font-weight:bold;"><a href="<?php echo $_ENV['xiaoneiUrl']."share.php";?>">去瘦身中心去转转</a>
				</span>
			</li>
            <li><span style="color:#FF6600;font-size:12px;">脂肪系数: <?php echo $helper->getUserFat();?></span></li>
            <li><span style="color:#005AB4;font-size:12px;">符合您身材的参考标准：</span></li>
            <li><span style="color:#FF6600;font-size:12px;">保持您的体重：
				<?php echo $helper->userStandardStart."--".$helper->userStandardEnd."公斤";?></span></li>          
			<li><span style="color:#FF6600;font-size:12px;">脂肪系数:&nbsp;19--24之间</span></li>  
			<li><span style="color:#FF6600;font-size:12px;">您的三围是这样？&nbsp;<?php echo "胸围(".$helper->getChestStandard().")  腰围(".$helper->getWaistStandard().")  臀围(".$helper->getHipsStandard().")";?></span></li>
        </ul>
</div>
 <?php }else{ ?>
<div style="float:left;margin:2px;width:315px;height:150px;background:url(<?php echo $_ENV['imgUrl']?>/box_grey_315x150_bar.gif) no-repeat;">
 <div style="color:#363636;font-weight:bold;margin-left:10px;line-height:27px;font-size:18px;">健康指数：</div>
 <ul style="float:left;margin:40px 40px 40px 40px!important;">
            <li>
				<span style="color:#FF6600;font-size:14px;font-weight:bold;"><a href="<?php echo $_ENV['xiaoneiUrl']."share.php";?>">去瘦身中心去看看</a>
				</span>
			</li>
 </ul>
</div>
<?php }?>
<br/>

<div style="clear:both"></div>
<div style="float:left;margin:2px;width:315px;border:1px solid #e5f0f8;">
        <div style="height:27px;color:#0066FF;font-weight:bold;margin-left:10px;line-height:27px;font-size:18px;">
        女肥婆[有福气]TOP10
        </div>
	<?php
		 $sqlQuery = "select * from app_pp_user_health where status='1'  and sex='0' order by healthy_index desc LIMIT 0,10";
		 $result = mysql_db_query($database, $sqlQuery);
		 while($row = mysql_fetch_object($result)){
	?>
         <div style="margin:1px;border-bottom:1px #ccc dashed;">
	    <div style="float:left;margin:6px;border-bottom:0px #ccc dashed;"><a href="http://xiaonei.com/getuser.do?id=<?php echo $row->out_id ?>"><img src="<?php echo $row->head_url;?>"/></a></div>
	     <div style="float:left;margin:4px;border-bottom:0px #ccc dashed;">
			    BMI： <?php echo $row->healthy_index;?> <br/>
				姓名：<a href="http://xiaonei.com/getuser.do?id=<?php echo $row->out_id ?>"><?php echo $row->user_name;?></a> <br/>
				大学：<?php echo $row->university_name;?><br/>
				公司：<?php echo $row->company_name;?>
             </div>
             <div style="clear:both"></div>
        </div>
	<?php }?>
 </div>
 <div style="float:left;margin:2px;width:315px;border:1px solid #e5f0f8;">
        <div style="height:27px;color:#0066FF;font-weight:bold;margin-left:10px;line-height:27px;font-size:18px;">
        男肚皮[圆嘟嘟]TOP10
        </div>
	<?php
		 $sqlQuery = "select * from app_pp_user_health where status='1'  and sex='1' order by healthy_index desc LIMIT 0,10";
		 $result = mysql_db_query($database, $sqlQuery);
		 while($row = mysql_fetch_object($result)){
	?>
        <div style="margin:1px;border-bottom:1px #ccc dashed;">
		<div style="float:left;margin:6px;border-bottom:0px #ccc dashed;"><a href="http://xiaonei.com/getuser.do?id=<?php echo $row->out_id ?>"><img src="<?php echo $row->head_url;?>"/></a></div>
		<div style="float:left;margin:4px;border-bottom:0px #ccc dashed;">
			    BMI： <?php echo $row->healthy_index;?> <br/>
				姓名：<a href="http://xiaonei.com/getuser.do?id=<?php echo $row->out_id ?>"><?php echo $row->user_name;?></a> <br/>
				大学：<?php echo $row->university_name;?><br/>
				公司：<?php echo $row->company_name;?>
              </div>
              <div style="clear:both"></div>
        </div>
	<?php }?>
 </div>
<div style="clear:both"></div>
<?php //}
	mysql_close();
?>	  
	  